home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* tessellate.c - open a window, clear the background, and render
- * a simple nonconvex polygon with and without tessellation.
- * The untessellated polygon is drawn (incorrectly) on the left.
- * The corresponding tessellated object is drawn on the right.
- *
- * The tessellated object is stored in a display list.
- *
- * Escape key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- void tesserror( GLenum errno );
- void my_glBegin( GLenum mode);
- void my_glEnd( void);
- void my_glVertex2fv( const GLfloat *v);
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define NPOLYVERTS (sizeof(polyVerts)/sizeof(polyVerts[0]))
- #define COLOR_OFFSET 2 /* color offset in array of info about vertex */
- #define VSIZE 5
- /* X Y R G B */
- static GLfloat polyVerts[][VSIZE]={
- { -2, 2, 0, 0, 0 },
- { -2, -2, 0, 0, 1 },
- { 2, -2, 0, 1, 0 },
- { 2, 2, 0, 1, 1 },
- { 0, 0, 1, 0, 0 }
- };
-
- /* Global Variables */
-
- static GLUtriangulatorObj *tObj = NULL;
- static GLuint tObjList;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - draws a simple nonconvex polygon twice;\n"
- "untessellated on the left, tessellated on the right\n\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- int i;
- GLdouble vertex[3];
-
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
-
- /* Create the tessellation object */
-
- tObj = gluNewTess();
-
- /* Set the tessellation callbacks */
-
- gluTessCallback(tObj, GLU_BEGIN, my_glBegin);
- gluTessCallback(tObj, GLU_VERTEX, my_glVertex2fv);
- gluTessCallback(tObj, GLU_END, my_glEnd);
- gluTessCallback(tObj, GLU_ERROR, tesserror);
-
- tObjList = glGenLists(1);
- glNewList( tObjList, GL_COMPILE );
- gluBeginPolygon(tObj);
- for ( i = 0; i < NPOLYVERTS; i++ ) {
- vertex[0] = polyVerts[i][0];
- vertex[1] = polyVerts[i][1];
- vertex[2] = 0;
- /* vertex[] is the vertex of the polygon
- * being tessellated. polyVerts[i] is
- * the vertex plus associated data
- * (color, in this case) that get passed
- * (by the GLU_VERTEX callback) to the
- * my_glVertex2fv func. */
- gluTessVertex(tObj, vertex, &polyVerts[i]);
- }
- gluEndPolygon(tObj);
- glEndList();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect, left, right, bottom, top;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- /* make sure the window goes from [-10.0, 10.0] in the
- * smallest dimension */
- if ( aspect < 1.0 ) {
- left = -10.0;
- right = 10.0;
- bottom = -10.0 * ( 1.0 / aspect );
- top = 10.0 * ( 1.0 / aspect );
- } else {
- left = -10.0 * aspect;
- right = 10.0 * aspect;
- bottom = -10.0;
- top = 10.0;
- }
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( left, right, bottom, top, -1.0, 1.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i;
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- /* Draw untessellated object (incorrectly) */
- glPushMatrix();
- glTranslatef( -5.0, 0.0, 0.0 );
- glBegin( GL_POLYGON );
- for (i=0; i < NPOLYVERTS; i++) {
- glColor3fv( polyVerts[i]+COLOR_OFFSET );
- glVertex2fv( polyVerts[i] );
- }
- glEnd();
- glPopMatrix();
-
- /* Draw tessellated objects (correctly) */
- glPushMatrix();
- glTranslatef( 5.0, 0.0, 0.0 );
- glCallList( tObjList );
- glPopMatrix();
-
- glFlush();
- checkError("drawScene");
- }
-
- /* The next four routines are for tessellation callbacks. */
-
- /* Called if there is an error during tessellation. */
- void
- tesserror ( GLenum errno )
- {
- printf( "tessellation error: %s\n", gluErrorString( errno ) );
- fflush( stdout );
- exit( -1 );
- }
-
- /* Called when a new primitive is started. */
- void
- my_glBegin(GLenum mode)
- {
- printf ("my_glBegin: mode is %d\n", mode);
- glBegin (mode);
- }
-
- /* Called when a primitive is finished. */
- void
- my_glEnd(void)
- {
- printf ("my_glEnd:\n");
- glEnd();
- }
-
- /* Called when a vertex of a primitive is generated by the
- * tessellator. The parameter v is a pointer to the data
- * that was passed to the GLU_VERTEX callback. In this case,
- * the first two elements are coordinates of the vertex
- * and the next three specify the vertex color. */
- void
- my_glVertex2fv(const GLfloat *v)
- {
- printf ("my_glVertex2fv: v[0] = %g, v[1] = %g ", v[0], v[1]);
- printf ("R = %g, G = %g, B = %g\n", v[2], v[3], v[4]);
-
- glColor3fv(v+COLOR_OFFSET);
- glVertex2fv (v);
- }
-